package com.gang.blog.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.gang.blog.domain.Article;
import com.gang.blog.domain.Category;
import com.gang.blog.domain.User;
import com.gang.blog.service.ArticleService;
import com.gang.blog.service.CategoryService;
import com.gang.blog.service.UserService;


@Controller
@RequestMapping("/article")
@Transactional
public class ArticleController {
	
	@Resource
	private ArticleService articleService;
	@Resource
	private CategoryService categoryService;
	@Resource
	private UserService userService;

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}
	
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
	

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * 发布文章
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/publish")
	public String publish(HttpServletRequest request,Model model){
		User user = (User) request.getSession().getAttribute("user");
		
			Article article = new Article();
			//设置article的信息
			article.setArticleTitle(request.getParameter("articleTitle"));
			//设置category属性
			article.setCategory(this.categoryService.getCategoryById(Integer.parseInt(request.getParameter("categoryId"))));
			article.setArticleContent(request.getParameter("articleContent"));
			article.setUser(user);
			article.setArticleDate(new Date());
			article.setArticleReadtime(0);
			//System.out.println(article);
			boolean mark = this.articleService.addArticle(article);
			if(mark){
				return "forward:/article/showArticleByPage";
			}else{
				return "error";
			}
		
		
	}
	/**
	 * 跳转到发布页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/gotoPublish")
	public String gotoPublish(HttpServletRequest request,Model model){
		User user = (User)request.getSession().getAttribute("user");
		if(user==null){
			return "error";
		}
		List<Category> categoryList = this.categoryService.getAllCategory();
		request.setAttribute("categoryList", categoryList);
		return "articleAdd";
	}
	/**
	 * 分页显示文章列表
	 * @param pageNum
	 * @param model
	 * @return
	 */
	@RequestMapping("/showArticleByPage")
	public String showArticleByPage(@Param("pageNow") Integer pageNow,Model model){
		//设置页面显示10条记录
		final Integer PAGESIZE = 10;
		List<Article> articleList = null;
		//判断要请求的页面数pageNum，
		if(pageNow==null){
			//pageNum为空表示首次访问页面，设置其值为0(设置为0，是因为)
			pageNow = 0;
			//获取对文章内容截取后的article列表
			articleList = this.articleService.leftFindByPage(pageNow, PAGESIZE);
		}else{
			//当pageNum不为空时，读取pageNow参数
			pageNow = pageNow-1;
			articleList = this.articleService.leftFindByPage(pageNow, PAGESIZE);
		}
		int pageCount = this.articleService.getPageCount(PAGESIZE);
		//将articleList，pageCount放入request中
		model.addAttribute("articleList",articleList);
		model.addAttribute("pageCount", pageCount);
		return "articleList";
	}
	
	/**
	 * 得到文章的全体信息
	 * @param articleId
	 * @param model
	 * @return
	 */
	@RequestMapping("/getArticleItem")
	public String getArticleItem(@Param("articleId") Integer articleId,Model model){
		Article article = this.articleService.getArticleItem(articleId);
		//System.out.println("/getArticleItem:"+ article.getComments());
		model.addAttribute("article", article);
		//System.out.println(article);
		return "articleItem";
	}
	
	/**
	 * 响应ajax全文显示请求
	 * @param articleId
	 * @return 返回一个存放有数据的HashMap对象
	 */
	@ResponseBody
	@RequestMapping(value="/getArticleContent",method=RequestMethod.GET)
	public Map<String, Object> getArticleContent(@Param("articleId") Integer articleId){
		//新建一个hashmap
		Map<String, Object> resultMap = new HashMap<String, Object>();
		//按文章id取得文章对象
		Article article = this.articleService.getSubStringArticle(articleId);
		//取出文章内容和文章id
		String articleContent = article.getArticleContent();
		Integer articleid = article.getArticleId();
		//将articleConnet和articleId放入hashmap中
		resultMap.put("articleContent", articleContent);
		resultMap.put("articleId", articleid);
		//List<Article> list = this.articleService.findByPage(1, 10);
		//resultMap.put("list", list);
		//返回hashmap到ajax
		return resultMap;
	}
	
	@RequestMapping(value="/showArticleByUser")
	public String showArticleByUser(@Param("pageNow") Integer pageNow,HttpServletRequest request,Model model){
		final Integer PAGESIZE = 10;
		List<Article> articleList = null;
		User user = (User) request.getSession().getAttribute("user");
		int userId = user.getUserId();
		//判断要请求的页面数pageNum，
		if(pageNow==null){
			//pageNum为空表示首次访问页面，设置其值为0(设置为0，是因为)
			pageNow = 0;
			//获取对文章内容截取后的article列表
			articleList = this.articleService.getArticleByUserId(userId, pageNow, PAGESIZE);
		}else{
			//当pageNum不为空时，读取pageNow参数
			pageNow = pageNow-1;
			articleList = this.articleService.getArticleByUserId(userId,pageNow, PAGESIZE);
		}
		int pageCount = this.articleService.getPageCountByUser(PAGESIZE);
		model.addAttribute("pageCount", pageCount);
		model.addAttribute("articleList", articleList);
		return "/personal/personalPage";
		
	}
	
}
